package com.zcr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcr.pojo.Jinhuorecords;
import com.zcr.pojo.Store;
import com.zcr.service.JinhuorecordsService;
import com.zcr.service.StoreService;
import com.zcr.vo.Result;
import org.apache.ibatis.annotations.Lang;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @version 1.0
 * @author： 张诚
 * @date： 2021-03-09 12:20
 */
@Controller
@RequestMapping("/jinhuoxinxi")
public class JinhuorecordsController {

    @Resource
    private JinhuorecordsService jinhuorecordsService;

    @Resource
    private StoreService storeService;

    @GetMapping("/jinhuojiru")
    public String login(){
        return "jinhuoxinxi";
    }

    @ResponseBody()
    @GetMapping("/pagexinxi")
    public Result getbyjinhuojiru(String drugname, int page, int limit){
        Page<Jinhuorecords> list = jinhuorecordsService.getbyjinhuoxinxi(drugname, page, limit);
        Result result = new Result();
        result.setData(list.getRecords());
        result.setCount(Integer.parseInt(Long.toString(list.getTotal())));
        return result;
    }

    @PostMapping("/addjinhuo")
    public Result add(Jinhuorecords jinhuorecords){

        Store store = new Store();
        store.setDrugname(jinhuorecords.getDrugname());
        store.setChangshang(jinhuorecords.getChangshang());
        store.setBeginprice(jinhuorecords.getBeginprice());
        store.setPrice(jinhuorecords.getPrice());
        store.setBegindate(jinhuorecords.getBegindate());
        store.setDate(jinhuorecords.getDate());
        store.setPihao(jinhuorecords.getPihao());
        store.setBeizhu(jinhuorecords.getBeizhu());
        store.setLocation(jinhuorecords.getLocation());
        store.setCount(jinhuorecords.getAmount());
        store.setUnit(jinhuorecords.getUnit());
        store.setGuige(jinhuorecords.getGuige());
        store.setTiaoxingma(jinhuorecords.getTiaoxingma());

        QueryWrapper<Jinhuorecords> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("drugname",jinhuorecords.getDrugname());
        queryWrapper.eq("changshang",jinhuorecords.getChangshang());
        queryWrapper.eq("begindate",jinhuorecords.getBegindate());
        Jinhuorecords list = jinhuorecordsService.getOne(queryWrapper);
        if(list != null){
        int countAmount =Integer.parseInt(jinhuorecords.getAmount())+Integer.parseInt(list.getAmount());
        list.setAmount(String.valueOf(countAmount));
        int countSum=Integer.parseInt(jinhuorecords.getSum())+Integer.parseInt(list.getSum());
        list.setSum(String.valueOf(countSum));
        jinhuorecordsService.update(list);
            QueryWrapper<Store> storeQueryWrapper=new QueryWrapper<>();
            storeQueryWrapper.eq("drugname",store.getDrugname());
            storeQueryWrapper.eq("changshang",store.getChangshang());
            storeQueryWrapper.eq("begindate",store.getBegindate());
            Store list2=storeService.getOne(storeQueryWrapper);
            int count =Integer.parseInt(jinhuorecords.getAmount())+Integer.parseInt(list.getAmount());
            list2.setCount(String.valueOf(count));
            storeService.update(list2);
            return Result.success("修改成功");
        }else{
            storeService.add(store);
            jinhuorecordsService.addjiru(jinhuorecords);
        }
        return Result.success("添加成功");
    }

}
